<template>
  <el-dialog
    v-model="visible"
    title="法规详情"
    width="900px"
    :before-close="handleClose"
  >
    <div v-if="regulation" class="regulation-detail">
      <!-- 基本信息 -->
      <div class="detail-section">
        <h3 class="section-title">基本信息</h3>
        <div class="info-grid">
          <div class="info-item">
            <label>法规名称：</label>
            <span class="regulation-title">{{ regulation.name }}</span>
            <el-tag v-if="regulation.isImportant" type="danger" size="small" style="margin-left: 8px">
              重要
            </el-tag>
          </div>
          <div class="info-item">
            <label>法规编号：</label>
            <span>{{ regulation.number }}</span>
          </div>
          <div class="info-item">
            <label>法规类型：</label>
            <el-tag :type="getTypeTagType(regulation.type)">
              {{ getTypeLabel(regulation.type) }}
            </el-tag>
          </div>
          <div class="info-item">
            <label>发布机构：</label>
            <span>{{ regulation.publisher }}</span>
          </div>
          <div class="info-item">
            <label>发布日期：</label>
            <span>{{ regulation.publishDate }}</span>
          </div>
          <div class="info-item">
            <label>生效日期：</label>
            <span>{{ regulation.effectiveDate }}</span>
          </div>
          <div class="info-item">
            <label>生效状态：</label>
            <el-tag :type="getStatusTagType(regulation.status)">
              {{ getStatusLabel(regulation.status) }}
            </el-tag>
          </div>
          <div class="info-item">
            <label>更新时间：</label>
            <span>{{ regulation.updateTime }}</span>
          </div>
        </div>
      </div>

      <!-- 法规摘要 -->
      <div class="detail-section" v-if="regulation.summary">
        <h3 class="section-title">法规摘要</h3>
        <div class="content-box">
          {{ regulation.summary || '暂无摘要信息' }}
        </div>
      </div>

      <!-- 法规内容 -->
      <div class="detail-section">
        <h3 class="section-title">法规内容</h3>
        <div class="content-box content-main">
          {{ regulation.content || '暂无详细内容' }}
        </div>
      </div>

      <!-- 关键词 -->
      <div class="detail-section" v-if="regulation.keywords">
        <h3 class="section-title">关键词</h3>
        <div class="keywords">
          <el-tag
            v-for="keyword in getKeywords(regulation.keywords)"
            :key="keyword"
            type="info"
            size="small"
            style="margin-right: 8px; margin-bottom: 8px;"
          >
            {{ keyword }}
          </el-tag>
        </div>
      </div>

      <!-- 相关附件 -->
      <div class="detail-section">
        <h3 class="section-title">相关附件</h3>
        <div class="attachments">
          <div v-if="mockAttachments.length > 0" class="attachment-list">
            <div
              v-for="attachment in mockAttachments"
              :key="attachment.id"
              class="attachment-item"
            >
              <el-icon class="attachment-icon"><Document /></el-icon>
              <span class="attachment-name">{{ attachment.name }}</span>
              <span class="attachment-size">{{ attachment.size }}</span>
              <el-button type="primary" size="small" @click="downloadAttachment(attachment)">
                下载
              </el-button>
            </div>
          </div>
          <div v-else class="no-attachments">
            <el-icon><Document /></el-icon>
            <span>暂无相关附件</span>
          </div>
        </div>
      </div>

      <!-- 修订历史 -->
      <div class="detail-section">
        <h3 class="section-title">修订历史</h3>
        <div class="revision-history">
          <el-timeline>
            <el-timeline-item
              v-for="revision in mockRevisions"
              :key="revision.id"
              :timestamp="revision.date"
              placement="top"
            >
              <div class="revision-item">
                <div class="revision-title">{{ revision.title }}</div>
                <div class="revision-desc">{{ revision.description }}</div>
                <div class="revision-author">修订人：{{ revision.author }}</div>
              </div>
            </el-timeline-item>
          </el-timeline>
        </div>
      </div>

      <!-- 备注信息 -->
      <div class="detail-section" v-if="regulation.remark">
        <h3 class="section-title">备注信息</h3>
        <div class="content-box">
          {{ regulation.remark }}
        </div>
      </div>
    </div>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">关闭</el-button>
        <el-button type="primary" @click="handleEdit">编辑</el-button>
        <el-button type="success" @click="handleDownload">下载文档</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { ElMessage } from 'element-plus'
import { Document } from '@element-plus/icons-vue'

// Props
interface Props {
  modelValue: boolean
  regulation?: any
}

const props = withDefaults(defineProps<Props>(), {
  regulation: null
})

// Emits
const emit = defineEmits<{
  'update:modelValue': [value: boolean]
  edit: [regulation: any]
}>()

// 计算属性
const visible = computed({
  get: () => props.modelValue,
  set: (value) => emit('update:modelValue', value)
})

// 模拟附件数据
const mockAttachments = ref([
  {
    id: 1,
    name: '中华人民共和国水法.pdf',
    size: '2.5MB',
    type: 'pdf'
  },
  {
    id: 2,
    name: '水法实施细则.doc',
    size: '1.8MB',
    type: 'doc'
  }
])

// 模拟修订历史数据
const mockRevisions = ref([
  {
    id: 1,
    title: '第三次修订',
    description: '完善水资源保护相关条款，增加生态环境保护要求',
    author: '张三',
    date: '2023-12-01 10:30:00'
  },
  {
    id: 2,
    title: '第二次修订',
    description: '调整水权管理制度，优化水资源配置机制',
    author: '李四',
    date: '2022-08-15 14:20:00'
  },
  {
    id: 3,
    title: '第一次修订',
    description: '增加水污染防治相关条款',
    author: '王五',
    date: '2020-03-10 09:15:00'
  },
  {
    id: 4,
    title: '初始版本',
    description: '法规首次发布',
    author: '系统',
    date: '2016-07-02 16:00:00'
  }
])

// 获取类型标签类型
const getTypeTagType = (type: string) => {
  const typeMap = {
    'national_law': '',
    'administrative_regulation': 'success',
    'departmental_rule': 'info',
    'local_regulation': 'warning',
    'technical_standard': 'danger',
    'management_system': ''
  }
  return typeMap[type] || ''
}

// 获取类型标签文本
const getTypeLabel = (type: string) => {
  const typeMap = {
    'national_law': '国家法律',
    'administrative_regulation': '行政法规',
    'departmental_rule': '部门规章',
    'local_regulation': '地方法规',
    'technical_standard': '技术标准',
    'management_system': '管理制度'
  }
  return typeMap[type] || type
}

// 获取状态标签类型
const getStatusTagType = (status: string) => {
  const statusMap = {
    'active': 'success',
    'pending': 'warning',
    'expired': 'info',
    'abolished': 'danger'
  }
  return statusMap[status] || ''
}

// 获取状态标签文本
const getStatusLabel = (status: string) => {
  const statusMap = {
    'active': '生效中',
    'pending': '待审核',
    'expired': '已失效',
    'abolished': '已废止'
  }
  return statusMap[status] || status
}

// 获取关键词数组
const getKeywords = (keywords: string) => {
  return keywords ? keywords.split(',').map(k => k.trim()).filter(k => k) : []
}

// 下载附件
const downloadAttachment = (attachment: any) => {
  ElMessage.info(`下载附件：${attachment.name}`)
}

// 编辑法规
const handleEdit = () => {
  emit('edit', props.regulation)
  handleClose()
}

// 下载文档
const handleDownload = () => {
  ElMessage.info('下载法规文档功能开发中...')
}

// 关闭对话框
const handleClose = () => {
  visible.value = false
}
</script>

<style scoped>
.regulation-detail {
  max-height: 70vh;
  overflow-y: auto;
}

.detail-section {
  margin-bottom: 24px;
}

.section-title {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
  margin: 0 0 16px 0;
  padding-bottom: 8px;
  border-bottom: 2px solid #e4e7ed;
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 16px;
}

.info-item {
  display: flex;
  align-items: center;
}

.info-item label {
  font-weight: 500;
  color: #606266;
  min-width: 80px;
  margin-right: 8px;
}

.regulation-title {
  font-weight: 600;
  color: #303133;
}

.content-box {
  background-color: #f8f9fa;
  border: 1px solid #e9ecef;
  border-radius: 4px;
  padding: 16px;
  line-height: 1.6;
  color: #495057;
}

.content-main {
  min-height: 120px;
  white-space: pre-wrap;
}

.keywords {
  display: flex;
  flex-wrap: wrap;
}

.attachments {
  border: 1px solid #e4e7ed;
  border-radius: 4px;
  padding: 16px;
}

.attachment-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.attachment-item {
  display: flex;
  align-items: center;
  padding: 12px;
  background-color: #f8f9fa;
  border-radius: 4px;
  gap: 12px;
}

.attachment-icon {
  font-size: 20px;
  color: #409eff;
}

.attachment-name {
  flex: 1;
  font-weight: 500;
  color: #303133;
}

.attachment-size {
  color: #909399;
  font-size: 12px;
}

.no-attachments {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 32px;
  color: #909399;
  gap: 8px;
}

.no-attachments .el-icon {
  font-size: 32px;
}

.revision-history {
  padding: 16px;
  background-color: #f8f9fa;
  border-radius: 4px;
}

.revision-item {
  padding-left: 16px;
}

.revision-title {
  font-weight: 600;
  color: #303133;
  margin-bottom: 4px;
}

.revision-desc {
  color: #606266;
  margin-bottom: 4px;
  line-height: 1.5;
}

.revision-author {
  color: #909399;
  font-size: 12px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

:deep(.el-timeline-item__timestamp) {
  color: #909399;
  font-size: 12px;
}
</style>